package com.kwum.phantomjs;

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * @Description: 
 * @Company: yimo
 * @author: Kwum
 * @date 2018年2月10日 下午4:31:40
 */

public class PhantomJSDemo {

	public static void main(String[] args) throws Exception {
		connect();
	}
	
	public static void connect() throws Exception{
		PhantomJSDriver driver = null;
        String path ="C:/phantomjs.exe";
        DesiredCapabilities dcaps = new DesiredCapabilities();
        //ssl证书支持
        dcaps.setCapability("acceptSslCerts", true);
        //截屏支持
        dcaps.setCapability("takesScreenshot", true);
        //css搜索支持
        dcaps.setCapability("cssSelectorsEnabled", true);
        //js支持
        dcaps.setJavascriptEnabled(true);
        dcaps.setBrowserName("Chrome");
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, path);
        //创建无界面浏览器对象
        driver = new PhantomJSDriver(dcaps);
        
        driver.get("http://m.uczzd.cn/ucnews/news?app=ucnews-iflow&aid=1146097850800475222&cid=408250330&zzd_from=ucnews-iflow&uc_param_str=dndsfrvesvntnwpfgibi&recoid=8808289219847918767&rd_type=reco");
        System.out.println(driver.getTitle());
        Thread.sleep(10 * 1000);
        System.out.println(driver.getPageSource());
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //讲截取的图片以文件的形式返回
	    try {
	        org.apache.commons.io.FileUtils.copyFile(srcFile, new File("d:\\screenshot1.png"));  //使用copyFile()方法保存获取到的截图文件
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
//        WebElement element = driver.findElement(By.xpath("//span[text()='下一页 »']"));
////        System.out.println(element.getTagName());
//        System.out.println(element.getText());
//        element.click();
//        Thread.sleep(5 * 1000);
//        srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //讲截取的图片以文件的形式返回
//	    try {
//	        org.apache.commons.io.FileUtils.copyFile(srcFile, new File("d:\\screenshot2.png"));  //使用copyFile()方法保存获取到的截图文件
//	    } catch (IOException e) {
//	        e.printStackTrace();
//	    }
//	    element = driver.findElement(By.xpath("//span[text()='下一页 »']"));
////      System.out.println(element.getTagName());
//	    System.out.println(element.getText());
//	    element.click();
//	    Thread.sleep(5 * 1000);
//	    srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //讲截取的图片以文件的形式返回
//	    try {
//	        org.apache.commons.io.FileUtils.copyFile(srcFile, new File("d:\\screenshot3.png"));  //使用copyFile()方法保存获取到的截图文件
//	    } catch (IOException e) {
//	        e.printStackTrace();
//	    }
	    driver.close();
        driver.quit();
	}
}
